package com.kds.changzhou.handler;

import com.kds.changzhou.data.Gateway;
import com.kds.changzhou.message.inbound.InBoundMessageFactory;
import com.kds.changzhou.message.outboud.OutBoundMessageFactory;
import com.kds.changzhou.netty.TcpClient;
import com.kds.changzhou.netty.TcpClientChannelInitializer;
import com.kds.changzhou.utility.DataQueryService;
import com.kds.changzhou.utility.ProtocolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * @author 孔德嵩
 */
@Configuration
public class HandlerConfig {

    @Autowired
    private ProtocolService protocolService;

    @Autowired
    private InBoundMessageFactory inBoundMessageFactory;

    @Autowired
    private OutBoundMessageFactory outBoundMessageFactory;

    @Autowired
    private DataQueryService dataQueryService;


    @Value("${ems-server.ip}")
    private String ip;

    @Value("${ems-server.port}")
    private int port;

    @Bean
    @Scope("prototype")
    public PackageDecoder getPackageDecoder() {
        return new PackageDecoder(this);
    }

    @Bean
    @Scope("prototype")
    public PackageEncoder getPackageEncoder() {
        return new PackageEncoder(this);
    }

    @Bean
    @Scope("prototype")
    public XmlDecoder getXmlDecoder() {
        return new XmlDecoder(this);
    }

    @Bean
    @Scope("prototype")
    public XmlEncoder getXmlEncoder() {
        return new XmlEncoder();
    }

    @Bean
    @Scope("prototype")
    public SequenceHandler getSequenceHandler() {
        return new SequenceHandler(this);
    }

    @Bean
    @Scope("prototype")
    public ConnectionStatusHandler getConnectionStatusHandler() {
        return new ConnectionStatusHandler(this);
    }

    @Bean
    @Scope("prototype")
    public ResultHandler getResultHandler() {
        return new ResultHandler(this);
    }

    @Bean
    @Scope("prototype")
    public TimeHandler getTimeHandler() {

        return new TimeHandler(this);
    }

    @Bean
    @Scope("prototype")
    public PeriodHandler getPeriodHandler() {

        return new PeriodHandler(this);
    }

    public TcpClientChannelInitializer getChannelInitializer() {
        TcpClientChannelInitializer clientChannelInitializer = new TcpClientChannelInitializer();

        clientChannelInitializer.setPackageDecoder(getPackageDecoder());
        clientChannelInitializer.setPackageEncoder(getPackageEncoder());
        clientChannelInitializer.setXmlDecoder(getXmlDecoder());
        clientChannelInitializer.setXmlEncoder(getXmlEncoder());
        clientChannelInitializer.setSequenceHandler(getSequenceHandler());
        clientChannelInitializer.setResultHandler(getResultHandler());
        clientChannelInitializer.setConnectionStatusHandler(getConnectionStatusHandler());
        clientChannelInitializer.setPeriodHandler(getPeriodHandler());
        clientChannelInitializer.setTimeHandler(getTimeHandler());
        return clientChannelInitializer;
    }


    public ProtocolService getProtocolService() {
        return protocolService;
    }

    public InBoundMessageFactory getInBoundMessageFactory() {
        return inBoundMessageFactory;
    }

    public OutBoundMessageFactory getOutBoundMessageFactory() {
        return outBoundMessageFactory;
    }

    public DataQueryService getDataQueryService() {
        return dataQueryService;
    }

    public String getIp() {
        return ip;
    }

    public int getPort() {
        return port;
    }

    public TcpClient createTcpClient(Gateway gateway) {

        return new TcpClient(this, gateway);
    }
}
